package com.hmys.api.service.impl;

import com.aliyun.oss.OSSClient;
import com.hmys.api.config.OSSConfig;
import com.hmys.api.service.UploadService;
import com.hmys.api.utils.OSSUtil;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.UUID;

/**
 * Created by chen365 on 2017/10/20.
 */

@Service
public class UploadServiceImpl implements UploadService {
    private static Logger logger = Logger.getLogger(UploadServiceImpl.class);
    @Autowired
    private OSSConfig ossConfig;

    @Override
    public String upload(String base64Data) throws Exception {

        String[] datas = base64Data.split("base64,");
        String data = "";
        String prefix = null;
        if (datas != null && datas.length == 2) {
            prefix = datas[0];
            data = datas[1];
        } else {
            throw new Exception("请选择图片");
        }

        String suffix = "";
        if ("data:image/jpeg;".equalsIgnoreCase(prefix)) {
            suffix = ".jpg";
        } else if ("data:image/x-icon;".equalsIgnoreCase(prefix)) {
            suffix = ".ico";
        } else if ("data:image/gif;".equalsIgnoreCase(prefix)) {
            suffix = ".gif";
        } else if ("data:image/png;".equalsIgnoreCase(prefix)) {
            suffix = ".png";
        } else {
            throw new Exception("上传图片格式不合法");
        }

        String filename = UUID.randomUUID().toString() + suffix;
        OSSClient ossClient = new OSSClient(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
        String md5key = OSSUtil.uploadObject2OSS(ossClient, data, filename, ossConfig.getBacketName());
        logger.info("上传后的文件MD5数字唯一签名:" + md5key);

        return ossConfig.getDomain() + filename;
    }

    @Override
    public String upload(File file) throws Exception {
        try {
            OSSClient ossClient = new OSSClient(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
            String md5key = OSSUtil.uploadObject2OSS(ossClient, file, ossConfig.getBacketName());
            logger.info("上传后的文件MD5数字唯一签名:" + md5key);
            FileUtils.deleteQuietly(file);
            String imageUrl = file.getName();
            // 返回"图像信息"选项卡并显示图片 ,在对应的文本框中显示图片资源url
            if (!StringUtils.isEmpty(imageUrl)) {
                imageUrl = ossConfig.getDomain() + imageUrl;
            }
            return imageUrl;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String upload(File file, String folder) throws Exception {
        try {
            OSSClient ossClient = new OSSClient(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
            String md5key = OSSUtil.uploadObject2OSS(ossClient, file, ossConfig.getBacketName(), folder);
            logger.info("上传后的文件MD5数字唯一签名:" + md5key);
            FileUtils.deleteQuietly(file);
            String imageUrl = file.getName();
            // 返回"图像信息"选项卡并显示图片 ,在对应的文本框中显示图片资源url
            if (!StringUtils.isEmpty(imageUrl)) {
                imageUrl = ossConfig.getDomain() + folder + "/" + imageUrl;
            }

            System.err.println("user.photo:"+imageUrl);
            return imageUrl;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
